<template>
  <div>
    <div class="f-row ai-c mb-4">
      <span class="mr-2">线上咨询</span>
      <el-switch
        v-model="consultOpen"
        :active-color="'#fd5151'"
        @change="change_consultOpen"
      >
      </el-switch>
    </div>
    <div class="f-row ai-c mb-4">
      <span class="mr-2">线下会诊</span>
      <el-switch
        v-model="registerOpen"
        :active-color="'#fd5151'"
        @change="change_registerOpen"
      >
      </el-switch>
    </div>
  </div>
</template>
<script>
import {
  show_status,
  online_switch,
  offline_switch,
  offline_switch_deep,
} from "@/request/api";
export default {
  data() {
    return {
      consultOpen: false,
      registerOpen: false,
    };
  },
  created() {
    this.getdata();
  },
  methods: {
    //获取状态
    getdata() {
      show_status()
        .then((res) => {
          console.log(res);
          this.consultOpen = res.result.consultOpen == 1 ? true : false;
          this.registerOpen = res.result.registerOpen == 1 ? true : false;
          console.log(this.consultOpen);
          console.log(this.registerOpen);
        })
        .catch((err) => {
          console.log(err, "获取专家服务状态失败");
        });
    },

    //线上咨询
    change_consultOpen() {
      online_switch()
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "修改成功",
              type: "success",
            });
            this.getdata();
          }
        })
        .catch((err) => {
          console.log(err, "线上咨询开关失败");
        });
    },
    //线下问诊
    change_registerOpen(e) {
      offline_switch()
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "修改成功",
              type: "success",
            });
            this.getdata();
          } else if (res.code == 99999) {
            this.registerOpen = !e;
            this.$confirm(res.message, "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                offline_switch_deep()
                  .then((res2) => {
                    if (res2.code == 200) {
                      this.$message({
                        type: "success",
                        message: "修改成功!",
                      });
                       this.getdata();
                    }
                  })
                  .catch((err) => {
                    console.log(err, "专家强制关闭线下会诊失败");
                  });
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消",
                });
              });
          }
        })
        .catch((err) => {
          console.log(err, "线下会诊开关失败");
        });
    },
    //强制关闭线下问诊
    change_deep() {},
  },
};
</script>